<template>
  <div>
    <Modal
      :title="title"
      :value="modalState"
      :loading="loading"
      @on-ok="handleSubmit"
      @on-cancel="cancel"
      width="35%">
      <Form
        ref="smsTemplateModal"
        :model="row"
        :rules="ruleInline"
        style="width: 100%; margin-left: 30px;"
        :label-width="110">
        <FormItem prop="tag" label="短信模板内容：" style="width: 75%">
          <Input v-model="row.tag" type="text" placeholder="请输入短信模板内容" />
        </FormItem>
        <FormItem prop="content" label="短信内容：" style="width: 75%">
          <Input v-model="row.content" type="textarea" :rows="6" placeholder="请输入短信内容" />
        </FormItem>
        <!-- <FormItem prop="des" label="备注：" style="width: 500px">
          <Input v-model="row.des" type="textarea" :rows="4" placeholder="请输入备注" />
        </FormItem> -->
        <!-- <FormItem prop="suffix" label="后缀内容：" style="width: 500px">
          <Input v-model="row.suffix" type="text" placeholder="请输入后缀内容" />
        </FormItem> -->
        <!-- <FormItem prop="openstate" label="是否启动：">
          <i-switch v-model="row.openstate" />
        </FormItem> -->
      </Form>
    </Modal>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
export default {
  name: 'SmsTemplateModal',
  data () {
    return {
      title: '',
      loading: true,
      ruleInline: {
        tag: [
          { required: true, message: '模板内容不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '短信内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  updated () {
    if (this.row.id && this.row.id !== undefined) {
      this.title = '编辑短信模板'
    } else {
      this.title = '添加短信模板'
    }
  },
  computed: {
    ...mapState({
      // row: state => state.taskList.row,
      modalState: state => state.modal.modalState,
      row: state => state.smsTemplate.row
    })
  },
  methods: {
    ...mapActions(['updateModalState']),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleSubmit () {
      this.$refs.smsTemplateModal.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.$emit('template-valid', {
          id: this.row.id,
          tag: this.row.tag,
          content: this.row.content
        })
        // this.$refs['smsTemplateModal'].resetFields()
      })
    },
    cancel () {
      this.updateModalState(false)
      this.$refs['smsTemplateModal'].resetFields()
    }
  }
}
</script>
